import api from "@/util/api";

async function findAll(pageNo = 1, pageSize = 10, params = {}) {
    try {
        let resp = await api({
            url: "/rbac-api/resource",
            method: "get",
            params: {
                pageNo,
                pageSize,
                ...params
            }
        });
        return resp;
    } catch (error) {
        console.error("API请求失败:", error);
        throw error;
    }
}

async function deleteById(id){
    let resp = await api({
        url:`/rbac-api/resource/deleteById/${id}`,
        method:"delete",
    })
    return resp;
}

async function save(resource){
    let resp = await api({
        url:"/rbac-api/resource/add",
        method:"post",
        data: resource
    })
    return resp;
}

async function update(resource){
    let resp = await api({
        url:"/rbac-api/resource/update",
        method:"put",
        data: resource
    })
    return resp;
}

async function findById(id){
    let resp = await api({
        url:`/rbac-api/resource/${id}`,
        method:"get",
    })
    return resp;
}

async function deleteAll(ids){
    let resp = await api({
        url:"/rbac-api/resource/delete",
        method:"delete",
        params: {
            ids: ids.join(',')
        }
    })
    return resp;
}

export {findAll, deleteById, save, update, findById, deleteAll}